﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.IO;
namespace watchdog
{
    class Program
    {
        static void Main(string[] args)
        {
            string filepath = Process.GetCurrentProcess().MainModule.FileName;
            int l = filepath.LastIndexOf('\\');
            if (l > 0)
            {
                filepath = filepath.Substring(0,  l+1);
            }

            StreamReader sr = new StreamReader(filepath+ "server.txt");
            string IP=sr.ReadLine();
            string sport = sr.ReadLine();
            sr.Close();
            int port=2345;
            int.TryParse(sport, out port);
            if (port == 0)
            {
                port = 2345;
            }
            while (true)
            {
                int wakeitup = 0;
                Process[] localByName = Process.GetProcessesByName("receiveSRV");
                if (localByName.Length == 0)
                {
                    wakeitup = 1;//try to call it again
                    Console.WriteLine("{0} - Cant locate process receiveSRV", DateTime.Now.ToString("yyyyMMdd HH:mm:ss"));
                }
                else
                {
                    connect con = new connect();
                    if (!con.isOpening(IP, port))
                    {
                        //try to close process
                        while (true)
                        {
                            Process[] pro = Process.GetProcessesByName("receiveSRV");
                            if (pro.Length > 0)
                            {

                                Console.WriteLine("{0} - Kill process {1}", DateTime.Now.ToString("yyyyMMdd HH:mm:ss"), pro[0].ProcessName);
                                try
                                {
                                    pro[0].Kill();
                                    while (!pro[0].HasExited)
                                    {
                                        Thread.Sleep(100);//delay 100 milisecond to wait for closing of the process
                                    }
                                }
                                catch (Exception ex)
                                {
                                    //do nothing
                                }
                               
                            }
                            else
                            {
                                break;
                            }
                        }
                        wakeitup = 1;
                    }
                    
                }
                if (wakeitup == 1)
                {
                    try
                    {
                        Console.WriteLine("{0} - Try to load receiveSRV.exe", DateTime.Now.ToString("yyyyMMdd HH:mm:ss"));
                        Process.Start(filepath + "receiveSRV.exe");
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("{0} - Load receiveSRV.exe error MES({1}) SOURCE({2})", DateTime.Now.ToString("yyyyMMdd HH:mm:ss"), ex.Message, ex.Source);
                    }
                }
                Thread.Sleep(6000);//waiting for 1 second
            }
        }
    }
}
